﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.product
{
    public partial class deliveryTemplateList : BaseAdmin
    {
        readonly DALDeliveryTemplate theDeliveryTemplate = new DALDeliveryTemplate();
        readonly DALDeliveryRegionRef theDeliveryRegionRef = new DALDeliveryRegionRef();
        StringBuilder sbRegionList;
        DataTable table;

        protected void Page_Load(object sender, EventArgs e)
        {


            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.DeliveryTemplate);
                this.btnDeliveryTemplateAdd.OnClientClick = Window1.GetShowReference("/admin/product/deliveryTemplate.aspx", "配送模板-添加");
               // this.btnRegin.OnClientClick = Window1.GetShowReference("/admin/product/deliveryRegion.aspx", "地区");

                this.DeleletDeliveryTemplate();
                this.BindGrid();


            }
        }
        private void BindGrid()
        {

             table = theDeliveryTemplate.GetList("").Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();

        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;

        }

        void DeleletDeliveryTemplate()
        {
            if (Request.QueryString["action"] == "del")
            {
                theDeliveryRegionRef.DeleteByDeliveryTemplateId(int.Parse(Request.QueryString["id"]));
               theDeliveryTemplate.Delete(int.Parse(Request.QueryString["id"]));
                ExtAspNet.Alert.Show("删除丁配送费用模板成功！");
            }
        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
             table =  this.theDeliveryTemplate .GetList("").Tables[0];
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

  

        protected void Window1_Close(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Grid1_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
            sbRegionList = new StringBuilder();
            table = theDeliveryRegionRef.GetList(" dr.deliveryTemplateId = " + this.Grid1.DataKeys[e.RowIndex][0].ToString()).Tables[0];
            int iIndex = 0;
            foreach (DataRow dr in table.Rows)
            {
                if (dr["depth"].ToString() == "1")
                {
                    sbRegionList.AppendFormat("<b>{0}</b><br/>", dr["regionName"].ToString());
                    iIndex = 0;
                    foreach (DataRow drSub in table.Rows)
                    {
                        iIndex++;
                        if (drSub["depth"].ToString() == "2" && dr["regionId"].ToString() == drSub["parentRegion"].ToString())
                        {
                            if (iIndex % 8 == 0)
                            {

                                sbRegionList.AppendFormat("{0}&nbsp;&nbsp;<br/>", drSub["regionName"].ToString());
                            }
                            else
                            {
                                sbRegionList.AppendFormat("{0}&nbsp;&nbsp;", drSub["regionName"].ToString());
                            }
                        }
                            
                    }
                }
            }
             e.Values[2] = sbRegionList.ToString();

        }





    }
}